﻿namespace WMS6.Web.CrossDock
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CrossdockServiceRef;

    public class Lane : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected DropDownList ddlLaneType;
        protected DropDownList ddlOhType;
        protected HtmlForm F79611;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLaneID;
        protected HtmlHead Head1;
        protected HtmlImage imgOwnerID0;
        protected Label lblDoor;
        protected Label lblLaneID;
        protected Label lblLaneSqe;
        protected Label lblLaneType;
        protected Label lblMsg;
        protected Label lblOhType;
        protected Label lblOwner;
        protected Label lblOwnerID;
        protected Label lblSku;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDoor;
        protected TextBox txtLaneID;
        protected TextBox txtLaneSeq;
        protected TextBox txtOwenrID;
        protected TextBox txtSku;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtLaneID.Text = "";
            this.txtLaneID.Enabled = true;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtLaneID.Text.Trim() != "")
                {
                    List<string> laneIDs = new List<string> {
                        this.txtLaneID.Text.Trim()
                    };
                    this.lblMsg.Text = CrossdockServiceClientProxy.GetInstance().DeleteLaneInfo(laneIDs) + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("LaneList.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("LaneList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtLaneID.Text))
            {
                this.lblMsg.Text = "请输入通道ID!";
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdateLaneInfo();
            }
            else
            {
                this.InsertLaneInfo();
            }
        }

        private LaneInfo GetLaneInfo(LaneInfo laneInfo)
        {
            int intTemp = 0;
            laneInfo.LaneID = this.txtLaneID.Text.Trim();
            if (int.TryParse(this.txtLaneSeq.Text.Trim(), out intTemp))
            {
                laneInfo.LaneSeq = int.Parse(this.txtLaneSeq.Text.Trim());
            }
            else
            {
                laneInfo.LaneSeq = 0;
            }
            laneInfo.OwnerID = this.txtOwenrID.Text.Trim();
            laneInfo.SkuID = this.txtSku.Text.Trim();
            laneInfo.LaneType = this.ddlLaneType.SelectedValue;
            laneInfo.OhType = this.ddlOhType.SelectedValue;
            laneInfo.Door = this.txtDoor.Text.Trim();
            laneInfo.IsActive = "Y";
            return laneInfo;
        }

        private void InsertLaneInfo()
        {
            try
            {
                LaneInfo laneInfo = new LaneInfo();
                laneInfo = this.GetLaneInfo(laneInfo);
                laneInfo.CreatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = CrossdockServiceClientProxy.GetInstance().InsertLaneInfo(laneInfo) + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = laneInfo;
                this.txtLaneID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlOhType, "OrderHandlingType ");
            base.BindData2DDLBySysCode(this.ddlLaneType, "LaneType ");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnLaneID.Value = base.Request.QueryString["ID"].ToString();
                this.SetLaneInfo2From();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetLaneInfo2From()
        {
            try
            {
                string laneID = this.hdnLaneID.Value;
                LaneInfo lane = CrossdockServiceClientProxy.GetInstance().GetLaneInfoByID(laneID);
                base.DataEntityViewState = lane;
                if (lane != null)
                {
                    this.txtLaneID.Text = lane.LaneID;
                    this.txtLaneID.Enabled = false;
                    this.txtOwenrID.Text = lane.OwnerID;
                    this.txtSku.Text = lane.SkuID;
                    this.txtLaneSeq.Text = lane.LaneSeq.ToString();
                    this.ddlLaneType.SelectedValue = lane.LaneType;
                    this.ddlOhType.SelectedValue = lane.OhType;
                    this.txtDoor.Text = lane.Door;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateLaneInfo()
        {
            try
            {
                LaneInfo laneInfo = base.DataEntityViewState as LaneInfo;
                laneInfo = this.GetLaneInfo(laneInfo);
                laneInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = CrossdockServiceClientProxy.GetInstance().UpdateLaneInfo(laneInfo) + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

